
############################
#### Adam Chilton      	####
#### Survey Experiment 	#### 
#### IL & Domestic Law  ####
#### 02/01/2014        	####
############################

######################################
####### Install & Load Libraries #####
######################################

install.packages("mediation")
install.packages("rms")
install.packages("sciplot")
install.packages("Hmisc")

library("Hmisc")
library("rms")
library("mediation")
library("sciplot")

###############################
####### Load the Data #########
###############################

data <- read.csv("Human Rights & Public Opinion_Data_01Feb14.csv", sep=",", header=TRUE)

########################################
####### Creating New Variables #########
########################################

#### Dependent Variable  #####

data$dv_complete[data$treatagree==1] <- 6
data$dv_complete[data$treatagree==2] <- 5
data$dv_complete[data$treatneith==1] <- 4
data$dv_complete[data$treatneith==2] <- 3
data$dv_complete[data$treatdis==2] <- 2
data$dv_complete[data$treatdis==1] <- 1

data <- data[data$dv_complete %in% c(1,2,3,4,5,6),]

#### Treatment Conditions #####

data$treat_dummy[data$dv2=="&nbsp"] <- "1. Control"
data$treat_dummy[data$dv2=="Critics of the use of solitary confinement argue that it should be eliminated except in the most extreme cases because it violates the human rights of the prisoners held in solitary confinement."] <- "2. Placebo"
data$treat_dummy[data$dv2=="Critics of the use of solitary confinement argue that it should be eliminated except in the most extreme cases because it violates international human rights treaties that the United States has signed."] <- "3. Int'l Law"

data$treat1_dummy[data$dv2=="&nbsp"] <- 1
data$treat1_dummy[data$dv2!="&nbsp"] <- 0

data$treat2_dummy[data$dv2=="Critics of the use of solitary confinement argue that it should be eliminated except in the most extreme cases because it violates the human rights of the prisoners held in solitary confinement."] <- 1
data$treat2_dummy[data$dv2!="Critics of the use of solitary confinement argue that it should be eliminated except in the most extreme cases because it violates the human rights of the prisoners held in solitary confinement."] <- 0

data$treat3_dummy[data$dv2=="Critics of the use of solitary confinement argue that it should be eliminated except in the most extreme cases because it violates international human rights treaties that the United States has signed."] <- 1
data$treat3_dummy[data$dv2!="Critics of the use of solitary confinement argue that it should be eliminated except in the most extreme cases because it violates international human rights treaties that the United States has signed."] <- 0


#### Demographic Variables #####

data$Male[data$gender==1] <- 1
data$Male[data$gender!=1] <- 0

data$College[data$educ_1==1] <- 0
data$College[data$educ_2==1] <- 0
data$College[data$educ_3==1] <- 0
data$College[data$educ_4==1] <- 0
data$College[data$educ_5==1] <- 1
data$College[data$educ_6==1] <- 1
data$College[data$educ_7==1] <- 1
data$College[data$educ_8==1] <- 1
data$College[data$educ_9==1] <- 1
data$College[data$educ_10==1] <- 1

data$repub[data$dem==1] <- 0
data$repub[data$dem==2] <- 0
data$repub[data$indp==5] <-0 
data$repub[data$indp==6] <- NA
data$repub[data$indp==4] <- 1
data$repub[data$repub==2] <- 1
data$repub[data$repub==1] <- 1

data$Race[data$race_1==1] <- "White"
data$Race[data$race_2==1] <- "Black"
data$Race[data$race_3==1] <- "Asian"
data$Race[data$race_4==1] <- "Hispanic"
data$Race[data$race_5==1] <- "Other"
data$Race[data$race_6==1] <- "Other"
data$Race[data$race_7==1] <- "Other"

#### Mechanism #####

data$intlstand_dummy[data$intlstand==1] <- 1
data$intlstand_dummy[data$intlstand==2] <- 1
data$intlstand_dummy[data$intlstand==3] <- 0
data$intlstand_dummy[data$intlstand==4] <- 0
data$intlstand_dummy[data$intlstand==5] <- 0

data$treaties_dummy[data$treaties==1] <- 1
data$treaties_dummy[data$treaties==2] <- 1
data$treaties_dummy[data$treaties==3] <- 0
data$treaties_dummy[data$treaties==4] <- 0
data$treaties_dummy[data$treaties==5] <- 0

data$moral_dummy[data$moral==1] <- 0
data$moral_dummy[data$moral==2] <- 0
data$moral_dummy[data$moral==3] <- 0
data$moral_dummy[data$moral==4] <- 1
data$moral_dummy[data$moral==5] <- 1


########################################
####### Subsetting Data 	############
########################################

data_dv1 <- data[data$treat_dummy %in% c("1. Control"),]
data_dv2 <- data[data$treat_dummy %in% c("2. Placebo"),]
data_dv3 <- data[data$treat_dummy %in% c("3. Int'l Law"),]

data_dem <- data[data$repub %in% c(0),]
data_rep <- data[data$repub %in% c(1),]

data_dem_dv1 <- data_dem[data_dem$treat_dummy %in% c("1. Control"),]
data_dem_dv2 <- data_dem[data_dem$treat_dummy %in% c("2. Placebo"),]
data_dem_dv3 <- data_dem[data_dem$treat_dummy %in% c("3. Int'l Law"),]

data_rep_dv1 <- data_rep[data_rep$treat_dummy %in% c("1. Control"),]
data_rep_dv2 <- data_rep[data_rep$treat_dummy %in% c("2. Placebo"),]
data_rep_dv3 <- data_rep[data_rep$treat_dummy %in% c("3. Int'l Law"),]



####################################################
####### Receipt of Treatment	 		############
####################################################

#### Correctly Got the Pro-Argument ####

table(data$Q320)
1769/(1769+43)

data$correct1[data$Q320==1]<-1
data$correct1[data$Q320==0]<-0

mydat <- sample(0:1,dim(data)[1],rep=TRUE,prob=c(0.5,0.5))

t.test(data$correct1, mydat)


#### Correctly Got the Treatment ####

table(data_dv1$Q321)
table(data_dv2$Q321)
table(data_dv3$Q321)

data$correct2[data$treat1_dummy==1 & data$Q321==3]<-1
data$correct2[data$treat2_dummy==1 & data$Q321==2]<-1
data$correct2[data$treat3_dummy==1 & data$Q321==1]<-1
data$correct2[data$treat1_dummy==1 & data$Q321!=3]<-0
data$correct2[data$treat2_dummy==1 & data$Q321!=2]<-0
data$correct2[data$treat3_dummy==1 & data$Q321!=1]<-0
table(data$correct2)

data$incorrect2 <- 0.3333333333333
t.test(data$correct2, data$incorrect2)



####################################################
####### IV.A. Primary Results 	    ############
####################################################


#### Creating Figure 1 ####
par(mar=c(6,5,1,1),mfrow=c(1,1)) 
lineplot.CI(x.factor = treat_dummy, response = dv_complete, data =  data,cex = 2, ylab = "Mean Response (Higher = Greater Support for Reform)",cex.lab = 1, xlab="Treatment Group", x.leg = 1, col = c("black","black"), pch = c(16,16), ylim=c(3.5,4.5),lty=0,ci.fun=function(x) c(mean(x)-1.645*se(x), mean(x)+1.645*se(x)))

#### Control Treatment ####
t.test(data_dv1$dv_complete,conf.level = 0.90)

#### Placebo Treatment ####
t.test(data_dv2$dv_complete,conf.level = 0.90)

#### International Law Treamtent ####
t.test(data_dv3$dv_complete,conf.level = 0.90)

#### Control Compared to International Law ####
t.test(data_dv1$dv_complete,data_dv3$dv_complete,conf.level = 0.90)

#### Placebo Compared to International Law ####
t.test(data_dv2$dv_complete,data_dv3$dv_complete,conf.level = 0.90)


###############################################################
####### IV.B. Results by Partisan Identification   ############
###############################################################

#### Creating Figure 2 ####
par(mar=c(4,4,3,1),mfrow=c(1,2)) 
lineplot.CI(x.factor = treat_dummy, response = dv_complete, data =  data_dem,cex = 2, ylab = "Mean Response (Higher = Greater Support for Reform)",xlab="Democrats", cex.lab = 1, x.leg = 1, col = c("black","black"), pch = c(16,16), ylim=c(2.5,5),lty=0,ci.fun=function(x) c(mean(x)-1.645*se(x), mean(x)+1.645*se(x)))
lineplot.CI(x.factor = treat_dummy, response = dv_complete, data =  data_rep,cex = 2, ylab = "", xlab="Republicans",cex.lab = 1, x.leg = 1, col = c("black","black"), pch = c(16,16), ylim=c(2.5,5),lty=0,ci.fun=function(x) c(mean(x)-1.645*se(x), mean(x)+1.645*se(x)))

#### Results for Democrats ####
t.test(data_dem_dv1$dv_complete,conf.level = 0.90)
t.test(data_dem_dv2$dv_complete,conf.level = 0.90)
t.test(data_dem_dv3$dv_complete,conf.level = 0.90)
mean(data_dem_dv3$dv_complete)-mean(data_dem_dv1$dv_complete)
t.test(data_dem_dv1$dv_complete,data_dem_dv3$dv_complete,conf.level = 0.90)

#### Results for Republicans ####
t.test(data_rep_dv1$dv_complete,conf.level = 0.90)
t.test(data_rep_dv2$dv_complete,conf.level = 0.90)
t.test(data_rep_dv3$dv_complete,conf.level = 0.90)
mean(data_rep_dv3$dv_complete)-mean(data_rep_dv1$dv_complete)
t.test(data_rep_dv1$dv_complete,data_rep_dv3$dv_complete,conf.level = 0.90)



####################################################
####### Figure 3			            ############
####################################################

##### Subsetting the Data for the Mechanism Questions ##### 
data_mech <- data[data$treat_dummy %in% c("1. Control","3. Int'l Law"),]
dim(data_mech)
data_mech$treatment[data_mech$treat_dummy=="1. Control"] <- 0
data_mech$treatment[data_mech$treat_dummy=="3. Int'l Law"] <- 1
table(data_mech$treatment)


########## Mechanism w/ Binary Response Variable #########

model.m <- lm(treaties_dummy ~ treatment, data=data_mech)
model.y <- lm(dv_complete ~ treatment+treaties_dummy, data = data_mech)
out.1 <- mediate(model.m, model.y, sims = 1000, treat= "treatment", conf.level=.90,mediator= "treaties_dummy")
summary(out.1)
plot(out.1)

model.m <- lm(moral_dummy ~ treatment, data=data_mech)
model.y <- lm(dv_complete ~ treatment+moral_dummy, data = data_mech)
out.2 <- mediate(model.m, model.y, sims = 1000, treat= "treatment", conf.level=.90,mediator= "moral_dummy")
summary(out.2)
plot(out.2)

model.m <- lm(intlstand_dummy ~ treatment, data=data_mech)
model.y <- lm(dv_complete ~ treatment+intlstand_dummy, data = data_mech)
out.3 <- mediate(model.m, model.y, sims = 1000, treat= "treatment", conf.level=.90,mediator= "intlstand_dummy")
summary(out.3)
plot(out.3)

######### Graphing Figure 3 #################

par(mar=c(3,6,1,1),mfrow=c(3,1),oma=c(2,1,2,1)) 
plot(out.1, main="Committment",xlim=c(-0.15,0.4))
plot(out.2, main="Morality",xlim=c(-0.15,0.4))
plot(out.3, main= "International Standards",xlim=c(-0.15,0.4))



####################################################
####### Appendix A - Summary Statistics ############
####################################################

summary(data$Male)
sd(data$Male)

dataage <- na.omit(data$Age)
summary(dataage)
sd(dataage)

datacollege <- na.omit(data$College)
summary(datacollege)
sd(datacollege)

datarepub <- na.omit(data$repub)
summary(datarepub)
sd(datarepub)

data$citizen2[data$citizen==1] <- 1
data$citizen2[data$citizen!=1] <- 0
summary(data$citizen2)
sd(data$citizen2)

data$White[data$Race=="White"] <- 1
data$White[data$Race!="White"] <- 0
datawhite <- na.omit(data$White)
summary(datawhite)
sd(datawhite)

data$Black[data$Race=="Black"] <- 1
data$Black[data$Race!="Black"] <- 0
datablack <- na.omit(data$Black)
summary(datablack)
sd(datablack)

data$Asian[data$Race=="Asian"] <- 1
data$Asian[data$Race!="Asian"] <- 0
dataasian <- na.omit(data$Asian)
summary(dataasian)
sd(dataasian)

data$Hispanic[data$Race=="Hispanic"] <- 1
data$Hispanic[data$Race!="Hispanic"] <- 0
datahispanic <- na.omit(data$Hispanic)
summary(datahispanic)
sd(datahispanic)

data$Other[data$Race=="Other"] <- 1
data$Other[data$Race!="Other"] <- 0
dataother <- na.omit(data$Other)
summary(dataother)
sd(dataother)


####################################################
####### Appendix C - Survey Balance	        ########
####################################################

logit1 <- lrm(treat1_dummy ~  Male+Age+College+repub+citizen+as.factor(Race), data=data)
print(logit1, digits=2)

logit2 <- lrm(treat2_dummy ~  Male+Age+College+repub+citizen+as.factor(Race), data=data)
print(logit2, digits=2)

logit3 <- lrm(treat3_dummy ~  Male+Age+College+repub+citizen+as.factor(Race), data=data)
print(logit3, digits=2)

####################################################
####### Appendix D - OLogit		            ########
####################################################


ologit <- lrm(as.factor(dv_complete) ~  treat2_dummy+treat3_dummy+Male+Age+College+repub+citizen+as.factor(Race), data=data)
print(ologit, digits=2)

